{extend name="public:index"/}{block name="title"}{:sysconf('title')}{/block}{block name="content"}
<section class="shouhuobox">
    <div class="top"><p class="p1"><a href="javascript:history.back(-1)"></a></p>
        <p class="p2">收货地址</p>
        <p class="p3" style="text-align:center;">编辑</p></div>
    <div style="height:2rem;"></div>
    {if condition="$list"}
    <div class="ddivbox"> {foreach name="$list" item="vo"}
        <div class="ddbox" style="position: relative;">
            <p class="p2 {eq name='vo.is_default' value='1'}canp2{/eq}" data-id="{$vo.id}">
                <b>{$vo.name} {$vo.phone}</b> <span>{$vo.province}{$vo.city}{$vo.area}{$vo.info}</span>
            </p>
            <p data-id="{$vo.id}" class="delete">删除</p>
            <a href="{:url('User/saveAddress',['addressId'=>$vo.id,'action'=>'edit'])}"><p class="p3"></p></a>
        </div>
        {/foreach}
    </div>
    {else/}
    <div class="nofind"><img src="__INDEX__/images/nodz.png" alt="">
        <p>暂时没有收货地址</p></div>
    {/if}
    <div class="xinzeng"><a href="{:url('User/saveAddress',['action'=>'add'])}">新增地址</a></div>
</section>
<style type="text/css">
    .delete {
        position: absolute;
        right: 0;
        top: 0;
        background-color: #ff0000;
        color: #fff;
        height: 3rem;
        padding: 0 1rem;
        line-height: 3rem;
        display: none;
    }
</style>
<script type="text/javascript">
    $('.shouhuobox .top .p3').click(function () {
        if ($(this).hasClass('ppp')) {
            $(this).removeClass('ppp');
            $('.delete').hide();
            $(this).html('编辑');
        } else {
            $(this).addClass('ppp');
            $('.delete').show();
            $(this).html('完成');
        }
    });
    $(document).on("click", ".shouhuobox .ddivbox .ddbox .p2", function () {
        var obj = this;
        var addressId = $(obj).data('id');
        $.ajax({
            url: "{:url('User/saveAddress')}",
            type: "POST",
            data: {'addressId': addressId, 'isDefault': 1},
            dataType: "JSON",
            success: function (data) {
                if (data.code == 0) {
                    $('.shouhuobox .ddivbox .ddbox .p2').removeClass('canp2');
                    $(obj).addClass('canp2');
                    window.location.href=document.referrer;
                } else {
                    layer.msg(data.msg, {'icon': 5});
                }
            },
            error: function () {
                layer.msg("网络延迟，发送失败", {icon: 5});
            }
        });
    });
    $(".delete").click(function () {
        var obj = this;
        var addressId = $(obj).data('id');
        $.ajax({
            url: "{:url('User/delAddress')}",
            type: "POST",
            data: {'addressId': addressId},
            dataType: "JSON",
            success: function (data) {
                if (data.code == 0) {
                    $(obj).parent().remove();
                } else {
                    layer.msg(data.msg, {'icon': 5});
                }
            },
            error: function () {
                layer.msg("网络延迟，发送失败", {icon: 5});
            }
        });
    });</script>{/block}